﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SoftwareConsulting.BI11.Studio.Core.Project.Metadata;

namespace SoftwareConsulting.BI11.Studio.DataTransfer
{
    /// <summary>
    /// Класс экспортера данных задачи классификации с обучением
    /// </summary>
    public class ClassificationProblemWithTrainingExporter
    {
        private IRepository _repository; //репозиторий
        private IClassificationProblemWithTraining _problem; //задача классификации с обучением

        /// <summary>
        /// Экспортирует указанные данные об объектах классификации в новую таблицу
        /// </summary>
        /// <param name="infos"></param>
        /// <param name="withClasses">true - с информацией о классах, false - без таковой</param>
        /// <returns></returns>
        public ITable ExportToTable(List<ClassificationObjectInfo> infos, bool withClasses)
        {
            if (infos.Count == 0)
                throw new BIClassificationProblemWithTrainingNoExportDataException(null);
            DataTransmitter transmitter = new DataTransmitter(DataNodeType.Directly, DataNodeType.Table);
            DirectDataNode source = new DirectDataNode();            
            object[] record = null;
            for (int i = 0; i < infos.Count; i++)
            {
                var info = infos[i];
                record = new object[info.Attributes.Length + (withClasses ? 1 : 0)];
                if (withClasses)
                    record[0] = _problem.ClassesNames.GetIndicatorValueByPosition(info.Class + 1).Value as string;//+1, т.к. первое значение - [не важно]
                info.Attributes.CopyTo(record, withClasses ? 1 : 0);
                source.WriteData(record);
            }
            source.SetReady();
            Type[] types = new Type[record.Length];
            string[] names = new string[record.Length];
            if (withClasses)
                names[0] = _problem.ClassesNames.Label;
            for (int i = 0; i < record.Length; i++)
            {
                types[i] = record[i].GetType();
                if ((i > 0) || !withClasses)
                    names[i] = string.Format("attribute{0}", i + (withClasses ? 0 : 1));
            }
            TableDataNode destination = new TableDataNode(_repository);
            destination.SetTable(types, names);
            if (destination.Table == null)
                throw new BIClassificationProblemWithTrainingExportCanceledException(null);            
            transmitter.SetSourceDataNode(source);
            transmitter.SetDestinationDataNode(destination);
            if (transmitter.Transfer())
                return destination.Table;
            return null;
        }

        /// <summary>
        /// Конструктор класса
        /// </summary>
        /// <param name="problem"></param>
        public ClassificationProblemWithTrainingExporter(IClassificationProblemWithTraining problem, IRepository repository)
        {
            _problem = problem;
            _repository = repository;
        }
    }
}
